<?php

class Xzsdsplus_Costpriceplus_Model_Resource_Mysql4_Rewrite_Product_Indexer_Price_Configurable
    extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Indexer_Price_Configurable
{
    
    protected function _prepareFinalPriceData($entityIds = null)
    {
        $this->_prepareDefaultFinalPriceTable();

        $write  = $this->_getWriteAdapter();
        $select = $write->select()
            ->from(array('e' => $this->getTable('catalog/product')), array('entity_id'))
            ->join(
                array('cg' => $this->getTable('customer/customer_group')),
                '',
                array('customer_group_id'))
            ->join(
                array('cw' => $this->getTable('core/website')),
                '',
                array('website_id'))
            ->join(
                array('cwd' => $this->_getWebsiteDateTable()),
                'cw.website_id = cwd.website_id',
                array())
            ->join(
                array('csg' => $this->getTable('core/store_group')),
                'csg.website_id = cw.website_id AND cw.default_group_id = csg.group_id',
                array())
            ->join(
                array('cs' => $this->getTable('core/store')),
                'csg.default_store_id = cs.store_id AND cs.store_id != 0',
                array())
            ->join(
                array('pw' => $this->getTable('catalog/product_website')),
                'pw.product_id = e.entity_id AND pw.website_id = cw.website_id',
                array())
            ->joinLeft(
                array('tp' => $this->_getTierPriceIndexTable()),
                'tp.entity_id = e.entity_id AND tp.website_id = cw.website_id'
                    . ' AND tp.customer_group_id = cg.customer_group_id',
                array())
            ->where('e.type_id=?', $this->getTypeId());

        // add enable products limitation
        $statusCond = $write->quoteInto('=?', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
        $this->_addAttributeToSelect($select, 'status', 'e.entity_id', 'cs.store_id', $statusCond, true);

        $taxClassId = $this->_addAttributeToSelect($select, 'tax_class_id', 'e.entity_id', 'cs.store_id');
        $select->columns(array('tax_class_id' => $taxClassId));

        $cppGroup       = $this->_addAttributeToSelect($select, 'cpp_group', 'e.entity_id', 'cs.store_id'); 
        
        $select->joinLeft(
            array('cppg' => $this->getTable('costpriceplus/groups')),
            "cppg.entity_id = {$cppGroup}",
            array() );
        
        $select->joinLeft(
            array('cppp' => $this->getTable('costpriceplus/product_price_index')),
            "cppp.product_id = e.entity_id AND cppp.website_id = cw.website_id",
            array() );
         
        $price              = $this->_addAttributeToSelect($select, 'price', 'e.entity_id', 'cs.store_id'); 
        $newPrice           = new Zend_Db_Expr("IF( (IFNULL({$cppGroup}, 0) <> 0) AND (cppg.is_active = 1) AND (cppp.price IS NOT NULL), cppp.price, {$price})");                                           
        
        $specialPrice   = $this->_addAttributeToSelect($select, 'special_price', 'e.entity_id', 'cs.store_id');
        $specialFrom    = $this->_addAttributeToSelect($select, 'special_from_date', 'e.entity_id', 'cs.store_id');
        $specialTo      = $this->_addAttributeToSelect($select, 'special_to_date', 'e.entity_id', 'cs.store_id');
        $curentDate     = new Zend_Db_Expr('cwd.date');

        $finalPrice     = new Zend_Db_Expr("IF(IF({$specialFrom} IS NULL, 1, "
            . "IF(DATE({$specialFrom}) <= {$curentDate}, 1, 0)) > 0 AND IF({$specialTo} IS NULL, 1, "
            . "IF(DATE({$specialTo}) >= {$curentDate}, 1, 0)) > 0 AND {$specialPrice} < {$newPrice}, "
            . "{$specialPrice}, {$newPrice})");
            
        $select->columns(array(
            'orig_price'    => $price,
            'price'         => $finalPrice,
            'min_price'     => $finalPrice,
            'max_price'     => $finalPrice,
            'tier_price'    => new Zend_Db_Expr('tp.min_price'),
            'base_tier'     => new Zend_Db_Expr('tp.min_price'),
        ));

        if (!is_null($entityIds)) {
            $select->where('e.entity_id IN(?)', $entityIds);
        }

        /**
         * Add additional external limitation
         */
        Mage::dispatchEvent('prepare_catalog_product_index_select', array(
            'select'        => $select,
            'entity_field'  => new Zend_Db_Expr('e.entity_id'),
            'website_field' => new Zend_Db_Expr('cw.website_id'),
            'store_field'   => new Zend_Db_Expr('cs.store_id')
        ));

        $query = $select->insertFromSelect($this->_getDefaultFinalPriceTable());
        $write->query($query);
        
        /**
         * Add possibility modify prices from external events
         */
        $select = $write->select()
            ->join(array('wd' => $this->_getWebsiteDateTable()),
                'i.website_id = wd.website_id',
                array());
        Mage::dispatchEvent('prepare_catalog_product_price_index_table', array(
            'index_table'       => array('i' => $this->_getDefaultFinalPriceTable()),
            'select'            => $select,
            'entity_id'         => 'i.entity_id',
            'customer_group_id' => 'i.customer_group_id',
            'website_id'        => 'i.website_id',
            'website_date'      => 'wd.date',
            'update_fields'     => array('price', 'min_price', 'max_price')
        ));

        return $this;
    }
    
    protected function _addConfigDataToSelect($select, $path, $website, $store)
    {
        $select
            ->joinLeft(
                array('default' => $this->getTable('core/config_data')),
                "default.path = '{$path}' AND default.scope = 'default'",
                array())
            ->joinLeft(
                array('websites' => $this->getTable('core/config_data')),
                "websites.path = '{$path}' AND websites.scope = 'websites' AND websites.scope_id = {$website}",
                array())
            ->joinLeft(
                array('stores' => $this->getTable('core/config_data')),
                "stores.path = '{$path}' AND stores.scope = 'stores' AND stores.scope_id = {$store}",
                array());
        return new Zend_Db_Expr("IFNULL(stores.value, IFNULL(websites.value, IFNULL(default.value, 1)))");
    }
} 